﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using NHibernate;
using RMH.DataAccess;

namespace RMH.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Patient",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Patient", action = "Index", id = "" }  // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            ControllerBuilder.Current.SetControllerFactory(new ControllerFactory());
        }


        public class ControllerFactory : DefaultControllerFactory
        {
            protected override IController GetControllerInstance(Type controllerType)
            {
                if (controllerType != null)
                {
                    return (IController)Container.Resolve(controllerType);
                }
                return null;
            }
        }

        public void Application_EndRequest()
        {
            Container.Resolve<ISessionFactoryFactory>().EndSession();
        }

        
    }
}